/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.server.promotion.logic;

import java.util.ArrayList;
import java.util.Vector;
import mscs.data.IFileDataStore;
import mscs.data.IToStringArrayCallBack;

/**
 *
 * @author Usbuild
 */
public abstract class GiftDiscountUtil<T> implements IGiftDiscountUtil<T> {
    protected IFileDataStore fds;
    protected ArrayList<T> list;
    public GiftDiscountUtil(IFileDataStore fds) {
        this.fds = fds;
        list = (ArrayList<T>)fds.getObjects();
    }
    public T queryByCriteria(String key, String value) {
        ArrayList<T> list = (ArrayList<T>)fds.getObjectsByAttr(key, value);
        if(list.isEmpty()) return null;
        return list.get(0);
    }
    abstract public Vector<Vector<String>> toDataVector();
    public Vector<Vector<String>> toDataVector(IToStringArrayCallBack callBack) {
        Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
        for(T type : list)
            dataVector.add(callBack.toStringArray(type));
        return dataVector;
    } 
    public void setByIndex(int index, T type) {
        list.set(index, type);
    }
    public ArrayList<T> getList() {
        return list;
    }
    public void addToList(T type) {
        list.add(type);
    }
    public void commit() {
        fds.update(list);
    }
    public void remove(int index) {
        list.remove(index);
    }
}
